package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class DamageConversionPane extends AdjustablePanel {
    private static String[] dmgTypes;
    private JLabel lblDmgConversionFrom;
    private JComboBox<String> cbDmgConversionFrom;
    private JLabel lblDmgConversionTo;
    private JComboBox<String> cbDmgConversionTo;

    static {
        DamageConversionPane.dmgTypes = new String[11];
        loadCBText();
    }

    public DamageConversionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblDmgConversionFrom).addComponent(this.lblDmgConversionTo)).addGroup(layout.createParallelGroup().addComponent(this.cbDmgConversionFrom).addComponent(this.cbDmgConversionTo));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblDmgConversionFrom).addComponent(this.cbDmgConversionFrom)).addGroup(layout.createParallelGroup().addComponent(this.lblDmgConversionTo).addComponent(this.cbDmgConversionTo));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, this.lblDmgConversionFrom, this.cbDmgConversionFrom);
        layout.linkSize(1, this.lblDmgConversionFrom, this.lblDmgConversionTo);
        layout.linkSize(1, this.lblDmgConversionFrom, this.cbDmgConversionTo);
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(60 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_DAMAGE_CONVERSION"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        loadCBText();
        if (this.lblDmgConversionFrom == null) {
            this.lblDmgConversionFrom = new JLabel();
        }
        this.lblDmgConversionFrom.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_DMG_CONV_FROM"));
        this.lblDmgConversionFrom.setFont(fntLabel);
        if (this.cbDmgConversionFrom == null) {
            this.cbDmgConversionFrom = new JComboBox<String>(DamageConversionPane.dmgTypes);
        } else {
            this.updateLanguage(this.cbDmgConversionFrom);
        }
        this.cbDmgConversionFrom.setFont(fntCombo);
        if (this.lblDmgConversionTo == null) {
            this.lblDmgConversionTo = new JLabel();
        }
        this.lblDmgConversionTo.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_DMG_CONV_TO"));
        this.lblDmgConversionTo.setFont(fntLabel);
        if (this.cbDmgConversionTo == null) {
            this.cbDmgConversionTo = new JComboBox<String>(DamageConversionPane.dmgTypes);
        } else {
            this.updateLanguage(this.cbDmgConversionTo);
        }
        this.cbDmgConversionTo.setFont(fntCombo);
    }

    private void updateLanguage(final JComboBox<String> cb) {
        String[] texts = null;
        if (cb == this.cbDmgConversionFrom) {
            texts = DamageConversionPane.dmgTypes;
        }
        if (cb == this.cbDmgConversionTo) {
            texts = DamageConversionPane.dmgTypes;
        }
        if (texts != null) {
            final int index = cb.getSelectedIndex();
            cb.removeAllItems();
            for (int i = 0; i < texts.length; ++i) {
                cb.addItem(texts[i]);
            }
            cb.setSelectedIndex(index);
        }
    }

    public void clear() {
        this.cbDmgConversionFrom.setSelectedIndex(0);
        this.cbDmgConversionTo.setSelectedIndex(0);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        int index = 0;
        String dmgType = null;
        index = this.cbDmgConversionFrom.getSelectedIndex();
        dmgType = this.getDamageTypeByIndex(index);
        criteria.dmgConversionFrom = dmgType;
        index = this.cbDmgConversionTo.getSelectedIndex();
        dmgType = this.getDamageTypeByIndex(index);
        criteria.dmgConversionTo = dmgType;
    }

    private String getDamageTypeByIndex(final int index) {
        String dmgType = null;
        switch (index) {
            case 1: {
                dmgType = "Poison";
                break;
            }
            case 2: {
                dmgType = "Aether";
                break;
            }
            case 3: {
                dmgType = "Chaos";
                break;
            }
            case 4: {
                dmgType = "Cold";
                break;
            }
            case 5: {
                dmgType = "Elemental";
                break;
            }
            case 6: {
                dmgType = "Fire";
                break;
            }
            case 7: {
                dmgType = "Lightning";
                break;
            }
            case 8: {
                dmgType = "Physical";
                break;
            }
            case 9: {
                dmgType = "Pierce";
                break;
            }
            case 10: {
                dmgType = "Life";
                break;
            }
        }
        return dmgType;
    }

    private static void loadCBText() {
        DamageConversionPane.dmgTypes[0] = "";
        DamageConversionPane.dmgTypes[1] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_POISON");
        DamageConversionPane.dmgTypes[2] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_AETHER");
        DamageConversionPane.dmgTypes[3] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_CHAOS");
        DamageConversionPane.dmgTypes[4] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_COLD");
        DamageConversionPane.dmgTypes[5] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_ELEMENTAL");
        DamageConversionPane.dmgTypes[6] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_FIRE");
        DamageConversionPane.dmgTypes[7] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_LIGHTNING");
        DamageConversionPane.dmgTypes[8] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_PHYSICAL");
        DamageConversionPane.dmgTypes[9] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_PIERCE");
        DamageConversionPane.dmgTypes[10] = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "OFF_LIFE");
    }
}
